//
//  StatuesViewModel.swift
//  Swift微博
//
//  Created by adel on 2017/8/28.
//  Copyright © 2017年 com.bailun91. All rights reserved.
//

import UIKit

class StatuesViewModel: NSObject {

    var status : statuesModel?
    var createAtText : String? //创建时间
    var sourceText: String?    //微博来源
    // MARK:- 用户数据
    var verifiedImage : UIImage? //用户认证图标
    var vipImage : UIImage?// 会员图标
    var profileURL :URL? //用户头像URL
    var picURLs : [URL] = [URL]()   // 处理微博配图的数据
    
    // MARK:- 自定义构造函数
     init(status: statuesModel) {//创建时间
        // 赋值
        self.status = status
        
        // 处理创建日期
        if let createAt = status.created_at {
              createAtText = NSDate.createDateString(createAtStr: createAt)
        }
        
        // 1.nil值校验
        if let source = status.source, source != ""{//处理微博来源
            // 1.1.获取起始位置和截取的长度
            let startIndex = (source as NSString).range(of: ">").location + 1
            let length = (source as NSString).range(of: "</").location - startIndex
            
            // 1.2.截取字符串
            sourceText = (source as NSString).substring(with: NSRange(location: startIndex, length: length))
        }
        
        //处理认证
        let verifiedType = status.user?.verified_type ?? -1
        
            switch verifiedType{
            case 0:
                verifiedImage = UIImage(named: "avatar_vip")
            case 2, 3, 5:
                verifiedImage = UIImage(named: "avatar_enterprise_vip")
            case 220:
                verifiedImage = UIImage(named: "avatar_grassroot")
            default:
                verifiedImage = nil
            }
        
        // 4.处理会员图标
        let mbrank = status.user?.mbrank ?? 0
        if mbrank > 0 && mbrank <= 6 {
            vipImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
        // 5.处理头像URL
        let profileURLString = status.user?.profile_image_url ?? ""
        profileURL = URL(string: profileURLString)
        
        
        // 6.处理配图数据
        let picURLDicts = status.pic_urls?.count != 0 ? status.pic_urls :status.retweeted_status?.pic_urls
        
        if let picURLDicts = picURLDicts {
            for picURLDict in picURLDicts {
                guard let picURLString = picURLDict["thumbnail_pic"] else {
                    continue
                }
                picURLs.append(URL(string: picURLString)!)
            }
        }
    }
    

    
}
